import axios from 'axios'
import config from '../config'

export function createKeyArea (name, lon, lat, usierid) {
  let url = config.SERVER_URL + window.apiPath + '/key-areas/createKeyArea'
  return axios.post(url, {name: name, longitude: lon, latitude: lat, userId: usierid, responseType: 'json'})
    .then(resp => resp.data)
}

export function deleteById (id) {
  let url = config.SERVER_URL + window.apiPath + '/key-areas/deleteKeyArea/' + id
  return axios.delete(url).then(resp => resp.data)
}

export function search (userId) {
  let url = config.SERVER_URL + window.apiPath + '/key-areas-s?query=userId==' + userId
  return axios.get(url).then(resp => resp.data)
}

// 使用方法
// 导入    import * as keyAreaService from '@/services/KeyAreaService'
// 第一种
/*
定义调用函数
getKeyArea(){
  let userId=this.$profile.id;
  return keyAreaService.search(userId);
}
异步调用上面的定义函数
getData() {
  this.shipgroup = [];
  this.getKeyArea().then((retlist) => {
    for (let k=0;k<retlist.length;k++){
      let oneobj={};
      oneobj.d_id=retlist[k].id;
      oneobj.lat=retlist[k].latitude;
      oneobj.lon=retlist[k].longitude;
      oneobj.name=retlist[k].name;
      this.shipgroup.push(oneobj);
    }
  }).then(() => {
    this.generateMap();
  });
}
*/
// 第二种，直接异步调用
// 函数前必须用 async 修饰，然后再函数中 增加 await 的修饰
/*
async suredel() {
  let retobj=await keyAreaService.deleteById(this.id);
  this.dialogDelete = false;
  this.getData();
}
*/
